package com._54programer.ws;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hazelcast.core.HazelcastInstance;

import lombok.Data;

@Component
@Data
public class CacheKey {

	private String key;
	private Object value;
	
	@Autowired
	private HazelcastInstance Hi;
	
	//一对一
	public void put(String key,Object value) {
		Hi.getMap(Const.DB0).put(key, value);
	}
	public Object get(String key) {
		return Hi.getMap(Const.DB0).containsKey(key) ? Hi.getMap(Const.DB0).get(key) : null;
	}
	
	
	// 一对多
	public void putMul(String key,Object value) {
		Hi.getMultiMap(Const.DB0).put(key, value);
	}
	public Object getMul(String key) {
		return Hi.getMultiMap(Const.DB0).containsKey(key) ? Hi.getMultiMap(Const.DB0).get(key) : null;
	}
	
	//List<Object>
	public void putSet(String miankey,String key,Object value) {
		CacheKey ck = new CacheKey();
		ck.setKey(key);
		ck.setValue(value);
		Hi.getSet(miankey).add(JSON.toJSON(ck));
	}

	public Object getSet(String miankey) {
		return Hi.getSet(miankey);
	}
	
	public Object getSet(String miankey,String key) {
		for (Object obj : Hi.getSet(miankey)) {
			CacheKey ck = JSON.toJavaObject(JSONObject.parseObject(obj.toString()), CacheKey.class);
			if(ck.getKey().equals(key) && Objects.equals(ck.getKey(), key)) {
				return ck.getValue();
			}
		}
		return null;
	}
	
}
